#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# Use this Dockerfile if the xserver-xspice base image is
# based on debian:stretch as it uses Python 2 dependencies.

FROM xserver-xspice

# Install eyeos spice-web-client.
RUN apt-get update && DEBIAN_FRONTEND=noninteractive \
    apt-get install -y --no-install-recommends \
    curl python-numpy python-setuptools && \
    # Download websockify and flexVDI fork of
    # eyeos spice-web-client.
    WS_VERSION=0.9.0 && \
    SPICE=3.1.0 && \
    curl -sSL https://github.com/novnc/websockify/archive/v${WS_VERSION}.tar.gz | tar -xzv -C /usr/local/bin && \
    curl -sSL https://github.com/flexVDI/spice-web-client/archive/${SPICE}.tar.gz | tar -xzv -C /usr/local/bin && \
    cd /usr/local/bin/websockify-${WS_VERSION} && \
    python setup.py install && \
    mv /usr/local/bin/spice-web-client-${SPICE} \
       /usr/local/bin/spice-web-client && \
    # Tweak spice-web-client to add a basic
    # host/port/password entry UI.
    sed -i 's/document.location.port/port/g' \
        /usr/local/bin/spice-web-client/lib/utils.js && \
    sed -i 's/<div class="float-right">/<div class="float-right">\n\n                <label for="host">Host: <\/label><input type="text" id="host" value=""\/><label for="port"> Port: <\/label><input type="text" id="port" value=""\/><label for="password"> Password: <\/label><input type="password" id="password" value="" onkeyup="checkIfEnterPressed(event)"\/>\n/g' /usr/local/bin/spice-web-client/index.html && \
    sed -i 's/$(document).ready(start);/$(document).ready(init);/g' /usr/local/bin/spice-web-client/run.js && \
    sed -i 's/translate();//g' \
        /usr/local/bin/spice-web-client/run.js && \
    sed -i 's/function start ()/\nfunction init () {\n	translate();\n    document.getElementById("showclientid").style.display = "none";\n    document.getElementById("uploadfile").style.display = "none";\n    document.getElementById("host").value = document.location.hostname;\n    document.getElementById("port").value = document.location.port;\n}\n\nfunction checkIfEnterPressed (event) {\n    if (event.keyCode === 13) {\n        start();\n    }\n}\nfunction start ()/g' /usr/local/bin/spice-web-client/run.js && \
    sed -i "s/data\['spice_address'\] || ''/document.getElementById('host').value/g" /usr/local/bin/spice-web-client/run.js && \
    sed -i "s/data\['spice_port'\] || 0/document.getElementById('port').value/g" /usr/local/bin/spice-web-client/run.js && \
    sed -i "s/data\['spice_password'\] || ''/document.getElementById('password').value/g" /usr/local/bin/spice-web-client/run.js && \
    # Create simple launch script to start pulseaudio,
    # xserver-xspice and the spice vdagent used to support
    # server resizing and copy/paste. Note that the exec
    # before /usr/bin/Xspice is important to ensure that
    # signals get correctly handled - without it the
    # container may hang on exit.
    echo '#!/bin/bash\npulseaudio &\nwebsockify 5800 localhost:5900 --web /usr/local/bin/spice-web-client -D\nexec /usr/bin/Xspice --auto $DISPLAY --password $(cat ${HOME}/.xserver-xspice-passwd) --xsession /usr/local/bin/xsession --vdagent --video-codecs ${SPICE_VIDEO_CODECS:-gstreamer:h264;gstreamer:vp8;gstreamer:mjpeg;spice:mjpeg} --audio-fifo-dir $XDG_RUNTIME_DIR/pulse > /dev/null' > /usr/local/bin/start-server && \
    chmod +x /usr/local/bin/start-server && \
    # Tidy up
    apt-get clean && \
    apt-get purge -y curl && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

CMD ["/usr/local/bin/start-server"]

#-------------------------------------------------------------------------------
# 
# To build the image
# docker build -t xserver-xspice-eyeos .
#

